import sys
import os
import shutil
import re

def remove_console(text):
	return re.sub('console.(log|debug)\((.*)\);?', '', text) 

me_filename = 'audioelement'
mep_filename = 'audioelementplayer'
combined_filename = 'audioelement-and-player'

# BUILD AudioElement (single file)

print('building AudioElement.js')
me_files = []
me_files.append('me-header.js')
me_files.append('me-namespace.js')
me_files.append('me-utility.js')
me_files.append('me-plugindetector.js')
me_files.append('me-featuredetection.js')
me_files.append('me-mediaelements.js')
me_files.append('me-shim.js')

code = ''

for item in me_files:
	src_file = open('js/' + item,'r')
	code += src_file.read() + "\n"
	
code = remove_console(code)

tmp_file = open('../build/' + me_filename + '.js','w')
tmp_file.write(code)
tmp_file.close()

# BUILD AudioElementPlayer (single file)
print('building AudioElementPlayer.js')
mep_files = []
mep_files.append('mep-header.js')
mep_files.append('mep-library.js')
mep_files.append('mep-player.js')
mep_files.append('mep-feature-playpause.js')
mep_files.append('mep-feature-progress.js')
mep_files.append('mep-feature-sourcechooser.js')
mep_files.append('mep-feature-time.js')
mep_files.append('mep-feature-volume.js')
mep_files.append('mep-feature-contextmenu.js')

code = ''

for item in mep_files:
    src_file = open('js/' + item,'r')
    code += src_file.read() + "\n"
        
code = remove_console(code)

tmp_file = open('../build/' + mep_filename + '.js','w')
tmp_file.write(code)
tmp_file.close()

# COMBINE into single script
print('Combining scripts')
code = ''
src_file = open('../build/' + me_filename + '.js','r')
code += src_file.read() + "\n"
src_file = open('../build/' + mep_filename + '.js','r')
code += src_file.read() + "\n"

tmp_file = open('../build/' + combined_filename + '.js','w')
tmp_file.write(code)
tmp_file.close()

#COPY skin files
print('Copying Skin Files')
shutil.copy2('css/controls.png','../build/controls.png')
shutil.copy2('css/controls.svg','../build/controls.svg')
shutil.copy2('css/audioelementplayer.css','../build/audioelementplayer.css')

print('DONE!')
